package com.welyo.model {
	import com.welyo.model.quizz.TabItemVO;
	import com.welyo.view.quizz.event.QuizzEvent;
	import flash.events.EventDispatcher;
	import flash.external.ExternalInterface;
	/**
	 * ...
	 * @author vu.doan
	 */
	public class QuizzModel extends EventDispatcher {
		
		private static var instance			:QuizzModel;
		
		private var totalTime				:Number;
		private var timeRemain				:Number;
		private var winLabel				:String;
		private var loseLabel				:String;
		private var titleQuestion			:String;
		private var namePlayerInput			:String;
		private var hightestLabel			:String;
		
		private var arrTab					:Array;
		private var currentTab				:int;
		private var totalMark				:int;
		private var totalComplete			:int;
		private var isWin					:Boolean;
		
		private var urlSaveQuizz			:String;
		
		public function QuizzModel() {
			if (QuizzModel.instance) {
				throw new Error('Use Model.getInstance() method instead');
			}
			QuizzModel.instance = this;
			arrTab = [];
			currentTab = -1;
			totalMark = 0;
			totalComplete = 0;
			isWin = false;
		}
		
		public static function getIntance():QuizzModel {
			if (!QuizzModel.instance) {
				QuizzModel.instance = new QuizzModel();
			}
			
			return instance;
		}
		
		public function parseData(data:XML):void {
			totalTime = parseFloat(data.totaltime.toString());
			winLabel = data.winlabel.toString();
			loseLabel = data.loselabel.toString();
			titleQuestion = data.titlequestion.toString();
			namePlayerInput = data.nameplayerinput.toString();
			hightestLabel = data.hightestlabel.toString();
			urlSaveQuizz = data.urlsavequizz.toString();
			
			var itm:TabItemVO;
			arrTab = [];
			for (var i:int = 0; i < data.tab.length(); i++) {
				itm = new TabItemVO(new XML(data.tab[i].toString()));
				arrTab.push(itm);
			}
		}
		
		public function get TitleQuestion():String {
			return titleQuestion;
		}
		
		public function get TotalTime():Number {
			return totalTime;
		}
		
		public function get WinLabel():String {
			return winLabel;
		}
		
		public function get LoseLabel():String {
			return loseLabel;
		}
		
		public function get NamePlayerInput():String {
			return namePlayerInput;
		}
		
		public function get HightestLabel():String {
			return hightestLabel;
		}
		
		public function get URLSaveQuizz():String {
			return urlSaveQuizz;
		}
		
		public function getTabAtIndex(index:int):TabItemVO {
			if (arrTab[index]) {
				return arrTab[index] as TabItemVO
			}
			
			return null;
		}
		
		public function set CurrentTab(value:int):void {
			currentTab = value;
			
			dispatchEvent(new QuizzEvent(QuizzEvent.CHANGE_TAB));
		}
		
		public function get CurrentTab():int {
			return currentTab;
		}
		
		public function startCountDown():void {
			dispatchEvent(new QuizzEvent(QuizzEvent.START_COUNTDOWN));
		}
		
		public function stopCountDown():void {
			dispatchEvent(new QuizzEvent(QuizzEvent.STOP_COUNTDOUWN));
		}
		
		public function last30():void {
			dispatchEvent(new QuizzEvent(QuizzEvent.START_LAST_30));
		}
		
		public function completeTestWith():void {
			totalComplete += 1;
			
			if (totalComplete == arrTab.length) {
				if (totalMark <= 100 || timeRemain == 0) {
					isWin = false;
				} else {
					isWin = true;
				}
				
				dispatchEvent(new QuizzEvent(QuizzEvent.COMPLETE_QUIZZ));
			} else {
				if (currentTab == 0) {
					CurrentTab = 1;
				} else if (currentTab == 1) {
					CurrentTab = 0;
				}
			}
		}
		
		private function getTotalQuestion():int {
			var total:int = 0;
			for (var i:int = 0; i < arrTab.length; i++) {
				total += (arrTab[i] as TabItemVO).Questions.length;
			}
			
			return total;
		}
		
		public function get TotalComplete():int {
			return totalComplete;
		}
		
		public function set TotalMark(value:int):void {
			totalMark = value;
			dispatchEvent(new QuizzEvent(QuizzEvent.CHANGE_MARK));
		}
		
		public function get TotalMark():int {
			return totalMark;
		}
		
		public function get IsWin():Boolean {
			return isWin;
		}
		
		public function set TimeRemain(value:Number):void {
			timeRemain = value;
			
			if (timeRemain == 0) {
				isWin = false;
				dispatchEvent(new QuizzEvent(QuizzEvent.COMPLETE_QUIZZ));
			}
		}
		
		public function get TimeRemain():Number {
			return timeRemain;
		}
		
		public function wrongAnswer():void {
			dispatchEvent(new QuizzEvent(QuizzEvent.WRONG_ANSWER));
		}
		
		public function correctAnswer():void {
			dispatchEvent(new QuizzEvent(QuizzEvent.CORRECT_ANSWER));
		}
		
		public function resetDataGame():void {
			totalMark = 0;
			timeRemain = totalTime;
			isWin = false;
			for (var i:int = 0; i < arrTab.length; i++) {
				(arrTab[i] as TabItemVO).HasComplete = false;
			}
			totalComplete = 0;
		}
	}
}